package com.springboot.quanxian.controller.admin;


import cn.dev33.satoken.stp.StpUtil;
import com.springboot.quanxian.common.ResultEnum;
import com.springboot.quanxian.entity.Notice;
import com.springboot.quanxian.exception.MyException;
import com.springboot.quanxian.qpage.QPage;
import com.springboot.quanxian.service.INoticeService;
import com.springboot.quanxian.util.ResultView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ResourceLoader;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import java.util.List;

/**
 * <p>
 * 公告表 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-04-16
 */
@RestController
@RequestMapping("/api/admin/notice")
public class NoticeController {



    @Autowired
    private INoticeService service;


    /**
     * 列表分页
     * @param obj
     * @return
     */
    @GetMapping("/getPageList")
    public ResultView getPageList(QPage qPage, Notice obj) {
        return this.service.getPageList(qPage,obj);
    }


    /**
     * 下拉不分页列表
     * @return
     */
    @GetMapping("/getSelectList")
    public ResultView getSelectList() {
        List<Notice> dtoList=this.service.getSelectList();
        return ResultView.ok(dtoList);
    }


    /**
     * 根据ID获取当前数据
     * @return
     */
    @GetMapping("/getById")
    public ResultView getById(@NotBlank(message = "ID不能为空") String id) {
        Notice obj=this.service.getById(id);
        return ResultView.ok(obj);
    }

    /**
     * 新增
     * @param obj
     * @return
     */
    @PostMapping("/add")
    public ResultView add(Notice obj) {
        boolean isOk= this.service.add(obj);
        if(!isOk){
            throw new MyException(ResultEnum.CODE_2,"新增失败，插入数据异常");
        }
        return ResultView.ok();
    }

    /**
     * 删除
     * @return
     */
    @PostMapping("/del")
    public ResultView del(@NotBlank(message = "ID不能为空") String id) {
        boolean isOk= this.service.del(id);
        if(!isOk){
            throw new MyException(ResultEnum.CODE_2,"删除失败，删除数据异常");
        }
        return ResultView.ok();
    }

    /**
     * 修改
     * @param obj
     * @return
     */
    @PostMapping("/up")
    public ResultView update(Notice obj) {
        boolean isOk= this.service.up(obj);
        if(!isOk){
            throw new MyException(ResultEnum.CODE_2,"修改失败，修改数据异常");
        }
        return ResultView.ok();
    }


}
